package com.wanhe.mealbookmodule.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.wanhe.mealbookmodule.adapter.views.IListViewAdapter;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.mealbookmodule.adapter.views.OnDataOperationResultListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public abstract class CommonBasePagerAdapter<M> extends PagerAdapter implements IListViewAdapter<M>{

    protected final String TAG = this.getClass().getSimpleName();
    private OnDataOperationResultListener mListener;

    public void setOnDataOperationResultListener(OnDataOperationResultListener listener) {
        this.mListener = listener;
    }

    public CommonBasePagerAdapter(Context context){
        this.mContext = context;
    }

    private Context mContext;

    protected List<M> mList = new ArrayList<>();

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    public M getItemAtPosition(int position){
        return mList.get(position);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (!ObjectUtil.checkObject(mContext)){
            throw new RuntimeException("CommonBaseAdapter don't hava context reference pleace call " +
                    "setContext(Context context) before ViewPager setAdater");
        }
        View view = genarateView(mContext, container, position);
        ViewGroup parent = (ViewGroup) view.getParent();
        if (ObjectUtil.checkObject(parent)){
            parent.removeView(view);
        }
        container.addView(view);
        return view;
    }

    protected abstract View genarateView(Context context, ViewGroup container, int position);


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //do nothing
    }

    @Override
    public void loadMore(List<M> list) {
        if (list != null && list.size() > 0) {
            this.mList.addAll(list);
            notifyDataSetChanged();
            if (mListener != null) {
                mListener.listLoaded(this);
            }
        } else {
            if (mListener != null) {
                mListener.noMoreData();
            }
        }
    }

    @Override
    public void refresh(List<M> list) {
        if (list != null && list.size() > 0) {
            this.mList.clear();
            this.mList.addAll(list);
            notifyDataSetChanged();
            if (mListener != null) {
                mListener.listLoaded(this);
            }
        } else {
            if (mListener != null) {
                mListener.empty();
            }
        }
    }
}
